/*
 * @Author: Clark
 * @Email: haixuanwoTxh@gmail.com
 * @Date: 2023-01-30 22:51:59
 * @LastEditors: Clark
 * @LastEditTime: 2023-01-30 22:59:03
 * @Description: file content
 */
#include <pthread.h>

struct msg{
    struct msg *m_next;
    // ... more stuff here ...
};

struct msg *workq;

pthread_cond_t qready = PTHREAD_COND_INITIALIZER;
pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER;

void process_msg(void)
{
    struct msg* mp;
    while (1)
    {
        pthread_mutex_lock(&qlock);
        while (NULL == workq)
        {
            pthread_cond_wait(&qready, &qlock);
        }

        mp = workq;
        workq = mp->m_next;
        pthread_mutex_unlock(&qlock);
        // now process the message mp
    }
}

void enqueue_msg(struct msg* mp)
{
    pthread_mutex_lock(&qlock);
    mp->m_next = workq;
    workq = mp;
    pthread_mutex_unlock(&qlock);

    pthread_cond_signal(&qready);
}
